package interview.algorithms.sort;

import common.Util;
import org.junit.Test;

/**
 * @author fance
 * @date 2018/7/12 19:22
 */
public class Again {

    void insertSort(int[] a, int n) {
        for (int i = 1; i < n; i++) {
            int tmp = a[i];
            int j = i;
            for (; j > 0 && a[j - 1] > tmp;j--) {
                a[j] = a[j - 1];
            }
            a[j] = tmp;
        }
    }

    void shellSort(int[] a, int n) {
        for (int increment = n / 2; increment > 0; increment /= 2) {
            for (int i = increment; i < n; i++) {
                int tmp = a[i];
                int j = i;
                for (; j >= increment;j -= increment) {
                     if (a[j] < a[j - increment]) {
                         a[j] = a[j - increment];
                     } else {
                         break;
                     }
                }
                a[j] = tmp;
            }
        }
    }

    void selectSort(int[] A, int n) {
        for (int i = 0; i < n - 1; i++) {
            int small = i;
            for (int j = i + 1; j < n; j++) {
                if (A[j] < A[small]) {
                    small = j;
                }
            }
            if (small != i) {
                Util.swap(A, i, small);
            }
        }
    }

    void bubbleSortI(int[] a, int n) {
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n - i; j++) {
                if (a[j] > a[j + 1]) {
                    Util.swap(a,j,j + 1);
                }
            }
        }
    }
    void bubbleSortII(int[] a, int n) {
        boolean flag = true;
        for (int i = 1; i < n && flag; i++) {
            flag = false;
            for (int j = 0; j < n - i; j++) {
                if (a[j] > a[j + 1]) {
                    flag = true;
                    Util.swap(a,j,j + 1);
                }
            }
        }
    }

    static int[] aux;
    // 自顶向下
    public void  mergeSort(int[] A, int n) {
        aux = new int[n];
        mergeSort(A, 0, n - 1);
    }
    public void  mergeSort(int[] A, int lo, int hi) {
        if (hi <= lo) {
            return;
        }
        int mid = lo + (hi - lo) / 2;
        mergeSort(A,lo,mid);
        mergeSort(A,mid + 1,hi);
        merge(A,lo,mid,hi);
    }
    public void merge(int[] a, int lo, int mid, int hi) {
        int i = lo, j = mid + 1;
        for (int k = lo; k <= hi; k++) {
            aux[k] = a[k];
        }
        for (int k = lo; k <= hi; k++) {
            if (i > mid) {
                a[k] = aux[j++];
            } else if (j > hi) {
                a[k] = aux[i++];
            } else if (aux[i] < aux[j]) {
                a[k] = aux[i++];
            } else {
                a[k] = aux[j++];
            }
        }
    }

    public void quickSort(int[] A, int n) {
        quickSort(A, 0, n - 1);
    }
    public void quickSort(int[] A, int lo, int hi) {
        if (hi <= lo) {
            return;
        }
        int j = partition(A, lo, hi);
        quickSort(A, lo, j - 1);
        quickSort(A, j + 1, hi);
    }
    public int partition(int[] a, int lo, int hi) {
        int i = lo,j = hi + 1;
        int v = a[lo];
        while (true) {
            while (a[++i] < v) {
                if (i == hi) {
                    break;
                }
            }
            while (a[--j] > v) {
                if (j == lo) {
                    break;
                }
            }

            if (i >= j) {
                break;
            }
            Util.swap(a,i,j);

        }
        Util.swap(a,lo,j);
        return j;
    }

    int leftChild(int i) {
        return 2 * i + 1;
    }
    void sink(int[] a,int i,int n) {
        int child;
        int tmp = a[i];
        for (; leftChild(i) < n; i = child) {
            child = leftChild(i);
            if (child != n - 1 && a[child + 1] > a[child]) {
                child++;
            }
            if (tmp < a[child]) {
                a[i] = a[child];
            } else {
                break;
            }
        }
        a[i] = tmp;
    }
    public void heapSort(int[] a,int n) {
        for (int i = n / 2; i >= 0; i--) {
            sink(a, i, n);
        }
        for (int i = n - 1; i > 0; i--) {
            // delMax
            Util.swap(a, 0, i);
            sink(a, 0, i);
        }
    }
    @Test
    public void test() {

        int[] a = {10, 23, 333,3, 101};
        mergeSort(a,a.length);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
}
